<style lang="less">
	page{
		background: #f6f6f6;
	}
	.content {
		text-align: center;
		margin-top: 20upx;
		padding: 0 30upx;
		box-sizing: border-box;
	}
	.main{
		background: #FFFFFF;
		width: 100%;
		min-height: 690upx;
		display: flex;
		justify-content: center;
		align-items: center;
		.main-img{
			.erweima{
				width: 490upx;
				height: auto;
			}	
			.title{
				margin-top: 20upx;
			}
		}
		
	}
	.title{
		font-size:26upx;
		color:rgba(153,153,153,1);
		line-height:39upx;
	}
</style>
<template>
	<view class="content">
		<view class="main">
			<view class="main-img"  v-if="imgShow==false">
				<image :src= "'https://static.xpandago.net'+'/'+img" class="erweima" mode="widthFix"></image>
				<view class="title">长按识别二维码，找到您的上级</view>
			</view>
			<view class="title" v-else>您的上级暂无上传二维码</view>
		</view>
	</view>
</template>
<script>
	import api from '@/common/api';
	export default {
		data() {
			return {
				token:'',
				img:'',
				imgShow:true
			}
		},
		onLoad() {
			this.token=uni.getStorageSync('token')
			console.log(this.token)
			let code=this.GetQueryString('code');
			console.log(code)
			var pages = getCurrentPages(); // 当前页面
			var beforePage = pages[pages.length - 1]; // 获取当前页面的对象
			var url = beforePage.route //当前页面url
			uni.setStorageSync('url', url);
			console.log(url)
			if(this.token!=null && this.token !='' && this.token != undefined){
				this.getParentWeChatCode()
			}else if(code!=null && code !='' && code != undefined){
				this.loginCode(code)
			}else{
				uni.showToast({
					title: '请先登录！',
					icon: 'none'
				})
				setTimeout(() => {
					uni.reLaunch({
					  url: "/pages/login/loginWechat"
					})
				}, 1000);
			}
		},
		methods: {
			async loginCode(code){
				const [err, res] = await api.loginCode({
						query: {
							code:code
						}
					 });
				 console.log(res)
				 if(res.data.code == 0){
					 let token=res.data.data
					 this.token=token
					 uni.setStorageSync('token', token);
					 this.getParentWeChatCode()
				 }else{
					 uni.showToast({
					 	title: res.data.msg,
					 	icon: 'none'
					 })
					if(res.data.code==100){
						uni.removeStorageSync('token');
						setTimeout(() => {
							uni.reLaunch({
							  url: "/pages/login/loginWechat"
							})
						}, 1000);
					}
				 }
			},
			GetQueryString(name){
			    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
			    var r = window.location.search.substr(1).match(reg);
			    if(r!=null)return  unescape(r[2]); return null;
			},
			async getParentWeChatCode(){
				const [err, res] = await api.getParentWeChatCode({
					 header:{
						'Content-Type': 'application/json',
						'token': this.token
					 }
				 });
				 console.log(res)
				 if(res.data.code == 0){
					 this.img=res.data.weChatCode
					 this.imgShow=false
				 }else{
					 uni.showToast({
					 	title: res.data.msg,
					 	icon: 'none'
					 })
					if(res.data.code==200){
						uni.removeStorageSync('token');
						setTimeout(() => {
							uni.reLaunch({
							  url: "/pages/login/loginWechat"
							})
						}, 1000);
					}
				 }
			}
		}
	}
</script>
